package pdi.segmentation;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import pdi.OpenCvCaller;
import pdi.core.BoundingBox;
import pdi.core.Image;
import utils.MyUtils;

public class ExtractGroundTruth extends AbstractSegmentation{

	private int propOfNegWindow = 10;
	private boolean generateNegWindows = false;
	private static final double scaleFactor = 0.4;
	
	
	public ExtractGroundTruth(){
		this(0);
	}
	
	public ExtractGroundTruth(int propOfNegWindow) {
		super();
		this.propOfNegWindow = propOfNegWindow;
		this.generateNegWindows = propOfNegWindow == 0 ? false: true;
	}

	@Override
	public List<Image> execute(Image sample) {
		List<Image> ret = new ArrayList<Image>();
		
		if(sample.getGroundTruth() != null && sample.getGroundTruth().size() > 0){
			for (BoundingBox bb : sample.getGroundTruth()) {
				ret.add(OpenCvCaller.getInstance().cropImage(sample, bb.getLeft(), bb.getTop(), bb.getWidth(), bb.getHeight()));
			}
		}else if(generateNegWindows){			
			for (int count = 0; count < propOfNegWindow; count++) {								
				
				
				//compute cordenations
				int height = MyUtils.getRandGen().nextInt((300 - 150) + 1) + 150;
				int width = (int)(height * 0.4);
				
				int maxX = sample.getWidth() - width < 0 ? 0 : sample.getWidth() - width;
				int maxY = sample.getHeight() - height < 0 ? 0 : sample.getHeight() - height;
				
				int x = MyUtils.getRandGen().nextInt(maxX + 1); 
				int y = MyUtils.getRandGen().nextInt(maxY + 1);
				
				
				//crop and add to the return list
				Image neg = OpenCvCaller.getInstance().cropImage(sample, x, y, width, height);
				neg.setName(MyUtils.addStringBFExt(neg.getName(), "_gen_" + count));
				ret.add(neg);
			}
		}
		
		return ret;
	}

}
